<template>
  <view class="s-row" :class="c_class" :style="c_style">
    <view class="s-row__wrap" :style="wrap_style">
      <slot />
    </view>
  </view>
</template>

<script>
import componentMixin from '../../mixins/componentMixin';

/**
 * s-row 栅格布局
 * @description 栅格组件父元素
 * @property {Boolean} vertical 是否竖向排列，默认false
 * @property {String} gutter 列元素主轴之间的间距（默认单位为 rpx）
 * @property {String} gutterY 列元素交叉轴之间的间距（默认单位为 rpx）
 * @property {String} justify = [start|center|end|space-around|space-between] Flex水平对齐方式，默认start
 * @property {String} align = [stretch|start|center|end] Flex垂直对齐方式，默认stretch，等高
 * @property {Number|String} height 同style中height属性
 * @property {String} overflow 同style中overflow属性
 * @example <s-row></s-row>
 */
export default {
  name: 'SRow',
  mixins: [componentMixin],
  props: {
    vertical: Boolean,
    justify: String,
    align: String,
    gutter: [Number, String],
    gutterY: [Number, String],
    height: [Number, String],
    overflow: String,
  },
  computed: {
    c_class() {
      return this.$mergeClass({
        's-row--vertical': this.vertical,
        [`s-row--justify-${this.justify}`]: this.justify,
        [`s-row--align-${this.align}`]: this.align,
      });
    },
    c_style() {
      return this.$mergeStyle({
        height: this.$addUnit(this.height),
        overflow: this.overflow,
      }, this.custom_style);
    },
    wrap_style() {
      const style = {};
      let { vertical, mainSpace, crossSpace } = this;
      if (mainSpace) {
        mainSpace = '-' + mainSpace;
        style[vertical ? 'marginTop' : 'marginLeft'] = mainSpace;
        style[vertical ? 'marginBottom' : 'marginRight'] = mainSpace;
      }
      if (crossSpace) {
        crossSpace = '-' + crossSpace;
        style[vertical ? 'marginLeft' : 'marginTop'] = crossSpace;
        style[vertical ? 'marginRight' : 'marginBottom'] = crossSpace;
      }
      return this.$mergeStyle(style);
    },
    mainSpace() {
      const mainSpace = parseInt(this.$toPx(this.gutter) / 2);
      return mainSpace > 0 ? mainSpace + 'px' : '';
    },
    crossSpace() {
      const crossSpace = parseInt(this.$toPx(this.gutterY) / 2);
      return crossSpace > 0 ? crossSpace + 'px' : '';
    },
  },
};
</script>

<style lang="scss" src="./index.scss"></style>
